﻿using System;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DataAccess
{
   public class KhachHangDAO
    {
        private static readonly String URL = ConfigurationSettings.AppSettings["Main.ConnectionString"];
        public KhachHangDAO()
        {

        }

        public static DataSet GetAll()
        {
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM KhachHang");
        }

        public static void Delete(int id) 
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaKhachHang", id);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "DELETE FROM KhachHang WHERE MaKhachHang = @MaKhachHang", sqlParameter);
        }

        public static void Insert(string tenkhahhang,string email,string diachi,string sodienthoai)
        {
            SqlParameter[] sqlParameter = new SqlParameter[4];
            sqlParameter[0] = new SqlParameter("@TenKhachHang", tenkhahhang);
            sqlParameter[1] = new SqlParameter("@Email", email);
            sqlParameter[2] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[3] = new SqlParameter("@SoDienThoai", sodienthoai);

            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "INSERT INTO KhachHang VALUES(@TenKhachHang, @Email,@DiaChi,@SoDienThoai)", sqlParameter);
        }

        public static void Update(int id, string tenkhahhang, string email, string diachi, string sodienthoai)
        {
            SqlParameter[] sqlParameter = new SqlParameter[5];
            sqlParameter[0] = new SqlParameter("@TenKhachHang", tenkhahhang);
            sqlParameter[1] = new SqlParameter("@Email", email);
            sqlParameter[2] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[3] = new SqlParameter("@SoDienThoai", sodienthoai);
            sqlParameter[4] = new SqlParameter("@MaKhachHang", id);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "UPDATE KhachHang SET TenKhachHang = @TenKhachHang,Email = @Email,DiaChi = @DiaChi,SoDienThoai=@SoDienThoai WHERE MaKhachHang = @MaKhachHang", sqlParameter);
        }
        public static DataSet GetBy(int id)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaKhachHang", id);
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM KhachHang WHERE MaKhachHang = @MaKhachHang", sqlParameter);
        }
    }
}
